<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="aaton">
    
    <title>
        
            Javascript之构造函数 |
        
        aaton&#39;s study notes
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="/images/logo.svg">
    
<link rel="stylesheet" href="/font/css/fontawesome.min.css">

    
<link rel="stylesheet" href="/font/css/regular.min.css">

    
<link rel="stylesheet" href="/font/css/solid.min.css">

    
<link rel="stylesheet" href="/font/css/brands.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {}
    KEEP.hexo_config = {"hostname":"example.com","root":"/","language":"en"}
    KEEP.theme_config = {"toc":{"enable":false,"number":false,"expand_all":false,"init_open":false},"style":{"primary_color":"#0066cc","logo":"/images/logo.svg","favicon":"/images/logo.svg","avatar":"/images/avatar.svg","font_size":null,"font_family":null,"hover":{"shadow":false,"scale":false},"first_screen":{"enable":true,"header_transparent":true,"background_img":"/images/bg.svg","description":"Keep writing and Keep loving.","font_color":null,"hitokoto":true},"scroll":{"progress_bar":false,"percent":false}},"local_search":{"enable":false,"preload":false},"code_copy":{},"code_block":{"tools":{"enable":true,"style":"default"},"highlight_theme":"default"},"side_tools":{},"pjax":{"enable":false},"lazyload":{"enable":false},"comment":{"enable":false,"use":"valine","valine":{"appid":null,"appkey":null,"server_urls":null,"placeholder":null},"gitalk":{"github_id":null,"github_admins":null,"repository":null,"client_id":null,"client_secret":null,"proxy":null},"twikoo":{"env_id":null,"region":null,"version":"1.6.8"},"waline":{"server_url":null,"reaction":false,"version":2}},"post":{"author_label":{"enable":true,"auto":true,"custom_label_list":["Trainee","Engineer","Architect"]},"word_count":{"enable":true,"wordcount":true,"min2read":true},"img_align":"left","copyright_info":false},"version":"3.6.1"}
    KEEP.language_ago = {"second":"%s seconds ago","minute":"%s minutes ago","hour":"%s hours ago","day":"%s days ago","week":"%s weeks ago","month":"%s months ago","year":"%s years ago"}
    KEEP.language_code_block = {"copy":"Copy code","copied":"Copied","fold":"Fold code block","folded":"Folded"}
    KEEP.language_copy_copyright = {"copy":"Copy copyright info","copied":"Copied","title":"Original article title","author":"Original article author","link":"Original article link"}
  </script>
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
<div class="progress-bar-container">
    

    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            
<header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
                <a class="logo-image" href="/">
                    <img src="/images/logo.svg">
                </a>
            
            <a class="logo-title" href="/">
               aaton&#39;s study notes
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                HOME
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                ARCHIVES
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/tags"
                            >
                                TAGS
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/categories"
                            >
                                CATEGORIES
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/about"
                            >
                                ABOUT
                            </a>
                        </li>
                    
                    
                </ul>
            </div>
            <div class="mobile">
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">HOME</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">ARCHIVES</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/tags">TAGS</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/categories">CATEGORIES</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/about">ABOUT</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="post-page-container">
        <div class="article-content-container">

            <div class="article-title">
                <span class="title-hover-animation">Javascript之构造函数</span>
            </div>

            
                <div class="article-header">
                    <div class="avatar">
                        <img src="/images/avatar.svg">
                    </div>
                    <div class="info">
                        <div class="author">
                            <span class="name">aaton</span>
                            
                                <span class="author-label">Lv3</span>
                            
                        </div>
                        <div class="meta-info">
                            
<div class="article-meta-info">
    <span class="article-date article-meta-item">
        
            <i class="fa-regular fa-calendar-plus"></i>&nbsp;
        
        <span class="pc">2023-06-05 11:00:11</span>
        <span class="mobile">2023-06-05 11:00</span>
    </span>
    
        <span class="article-update-date article-meta-item">
        <i class="fas fa-file-pen"></i>&nbsp;
        <span class="pc">2023-06-03 17:23:04</span>
    </span>
    
    
        <span class="article-categories article-meta-item">
            <i class="fas fa-folder"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/Javascript/">Javascript</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0/">构造函数</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
        <span class="article-wordcount article-meta-item">
            <i class="fas fa-file-word"></i>&nbsp;<span>983 Words</span>
        </span>
    
    
        <span class="article-min2read article-meta-item">
            <i class="fas fa-clock"></i>&nbsp;<span>3 Mins</span>
        </span>
    
    
        <span class="article-pv article-meta-item">
            <i class="fas fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                        </div>
                    </div>
                </div>
            

            <div class="article-content keep-markdown-body">
                

                <p><img src="https://cn.bing.com/th?id=OHR.GemsbokNamibia_ZH-CN0963988839_1920x1200.jpg&rf=LaDigue_1920x1200.jpg" alt="bing.com-丘中的南非剑羚，纳米比亚" title="bing.com-丘中的南非剑羚，纳米比亚"></p>
<p>Javascript中构造函数定义、使用方法、以及普通函数的区别。</p>
<span id="more"></span>

<p>所谓”构造函数”，就是专门用来生成实例对象的函数。它就是对象的模板，描述实例对象的基本结构。</p>
<p>一个构造函数，可以生成多个实例对象，这些实例对象都有相同的结构。都可以使用这个构造函数的属性与方法。</p>
<p>构造函数的特点：</p>
<ul>
<li>构造函数的this则是指向它创建的对象实例。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 普通函数中的this，在严格模式下指向undefined，非严格模式下指向window对象。</span><br><span class="line">function foo()&#123;</span><br><span class="line">	console.log(this===window);</span><br><span class="line">&#125;</span><br><span class="line">foo();</span><br><span class="line">//代码运行结果：true</span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">构造函数的this则是指向它创建的对象实例。</span><br><span class="line">function Foo()&#123;</span><br><span class="line">	this.name = &quot;令狐冲&quot;;</span><br><span class="line">&#125;</span><br><span class="line">var f = new Foo();</span><br><span class="line">console.log(f.name);</span><br><span class="line">//代码运行结果：令狐冲</span><br></pre></td></tr></table></figure>
<p><em><strong>构造函数的函数名和类名相同：Foo()这个构造函数，Foo是函数名，也是这个对象的类名。</strong></em></p>
<ul>
<li><p>调用（生成对象）的时候，必须使用new关键字。</p>
</li>
<li><p>一个构造函数可以使用 new 关键字来创造出若干的实例。</p>
</li>
<li><p>每一个实例都可以使用这个构造函数的属性和方法。</p>
</li>
</ul>
<hr>
<h3 id="1、构造函数的实现"><a href="#1、构造函数的实现" class="headerlink" title="1、构造函数的实现"></a>1、构造函数的实现</h3><p>构造函数其实本质也是一个普通函数，但是有自己的特征和用法。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">function Foo()&#123;&#125;</span><br><span class="line">Foo();//普通函数调用方式</span><br><span class="line">var f = new Foo();//构造函数调用方式</span><br></pre></td></tr></table></figure>
<ul>
<li>普通函数调用方式：直接调用person()。</li>
<li>构造函数调用方式：需要使用new关键字来调用 new person()。</li>
</ul>
<hr>
<h3 id="2、构造函数中的new关键字"><a href="#2、构造函数中的new关键字" class="headerlink" title="2、构造函数中的new关键字"></a>2、构造函数中的new关键字</h3><p>new 命令的作用，就是执行构造函数，返回一个实例对象。</p>
<ul>
<li>new 就是创造对象的过程。</li>
<li>new 也叫做实例化对象的过程。</li>
<li>new创造出来的对象叫做构造函数的实例对象。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">    function Person() &#123;</span><br><span class="line">    //构造函数体内的属性</span><br><span class="line">    this.name = &#x27;Jack&#x27;</span><br><span class="line">&#125;</span><br><span class="line">// Person.prototype 原型 上的方法</span><br><span class="line">Person.prototype = &#123;</span><br><span class="line">    a: function () &#123; </span><br><span class="line">        console.log(&#x27;我是一个a方法&#x27;)</span><br><span class="line">        &#125;,</span><br><span class="line">    b: function () &#123; &#125;,</span><br><span class="line">    // ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var p = new Person()</span><br><span class="line">console.log(p)</span><br><span class="line">console.log(p.name)</span><br><span class="line">p.a()</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200223151628831.png" alt="CSDN : JS——构造函数" title="CSDN : JS——构造函数"></p>
<ul>
<li>构造函数必须有new关键字。</li>
<li>P是一个变量，保存了Person的一个<strong>实例对象</strong>。</li>
<li>P从构造函数Person得到了name属性和Person.prototype上的方法。</li>
<li>new命令执行时，构造函数内部的this，就代表了新生成的实例对象，this.name表示实例对象有一个name属性，值是Jack。实例对象的a方法就是原型上的_proto_: a: f( )，console.log(我是一个a方法)。</li>
</ul>
<hr>
<h3 id="3、创建构造函数的方式"><a href="#3、创建构造函数的方式" class="headerlink" title="3、创建构造函数的方式"></a>3、创建构造函数的方式</h3><ul>
<li><p>属性写在构造函数体内。</p>
</li>
<li><p>方法写在构造函数的原型上。</p>
</li>
<li><p>方法不是不能写在构造函数体内<br>如果你写在构造函数体内，每次 new 的时候会创建一个函数空间，最后就会有若干个一摸一样的函数空间出现,浪费内存空间。</p>
</li>
</ul>
<hr>
<h3 id="4、构造函数的特殊情况"><a href="#4、构造函数的特殊情况" class="headerlink" title="4、构造函数的特殊情况"></a>4、构造函数的特殊情况</h3><ul>
<li><p>构造函数体内如果 return 返回值一个基本数据类型, 与new 关键字连用的时候, 得不到你 return 的基本数据类型。</p>
</li>
<li><p>构造函数体内如果 return 返回值是一个复杂数据类型, 构造函数无法创建一个实例对象，不管是否与new关键字连用。 得到的都是 return 的 复杂数据类型。</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">function Person() &#123;</span><br><span class="line">    this.name = &#x27;Jack&#x27;</span><br><span class="line">    this.age = 18</span><br><span class="line">    this.gender = &#x27;男&#x27;</span><br><span class="line">    </span><br><span class="line">    // return 1</span><br><span class="line">    // return [1, 2, 3]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Person.prototype = &#123;</span><br><span class="line">    a() &#123; &#125;,</span><br><span class="line">    b() &#123; &#125;,</span><br><span class="line">    c() &#123; &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var a = new Person() </span><br><span class="line">console.log(a)</span><br></pre></td></tr></table></figure>
<p>正常情况下：<br><img src="https://img-blog.csdnimg.cn/20200223153139990.png" alt="CSDN : JS——构造函数" title="CSDN : JS——构造函数"><br>返回一个基本数据类型 return 1 与new关键字连用之后结果不变:<br><img src="https://img-blog.csdnimg.cn/20200223153259872.png" alt="CSDN : JS——构造函数" title="CSDN : JS——构造函数"><br>返回一个复杂数据类型 return [1,2,3]<br><img src="https://img-blog.csdnimg.cn/20200223153350186.png" alt="CSDN : JS——构造函数" title="CSDN : JS——构造函数"></p>
<hr>
<p><em><strong>参考文献</strong></em></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://juejin.cn/post/6996583771952644110" >稀土掘金 ： JS中构造函数与普通函数的区别<i class="fas fa-external-link-alt"></i></a></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://blog.csdn.net/k464746/article/details/104406157" >CSDN : JS——构造函数<i class="fas fa-external-link-alt"></i></a></p>

            </div>

            

            
                <ul class="post-tags-box">
                    
                        <li class="tag-item">
                            <a href="/tags/%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0/">#构造函数</a>&nbsp;
                        </li>
                    
                </ul>
            

            
                <div class="article-nav">
                    
                        <div class="article-prev">
                            <a class="prev"
                               rel="prev"
                               href="/2023/06/05/Javascript%E4%B9%8B%E6%B5%85%E6%8B%B7%E8%B4%9D%E4%B8%8E%E6%B7%B1%E6%8B%B7%E8%B4%9D/"
                            >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                                <span class="title flex-center">
                                <span class="post-nav-title-item">Javascript之浅拷贝与深拷贝</span>
                                <span class="post-nav-item">Prev posts</span>
                            </span>
                            </a>
                        </div>
                    
                    
                        <div class="article-next">
                            <a class="next"
                               rel="next"
                               href="/2023/06/05/Javascript%E4%B9%8B%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/"
                            >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">Javascript之数据类型</span>
                                <span class="post-nav-item">Next posts</span>
                            </span>
                                <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                            </a>
                        </div>
                    
                </div>
            

            
        </div>

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            
<footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
                <span>2020</span> -
            
            2024
            
                &nbsp;<i class="fas fa-heart icon-animate"></i>
                &nbsp;<a href="/">aaton</a>
            
        </div>
        
            <script async 
                    src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    Visitor Count&nbsp;<span id="busuanzi_value_site_uv"></span>&ensp;
                
                
                    Totalview&nbsp;<span id="busuanzi_value_site_pv"></span>
                
            </div>
        
        <div class="theme-info info-item">
            Powered by <a target="_blank" href="https://hexo.io">Hexo</a>&nbsp;|&nbsp;Theme&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.6.1</a>
        </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="fas fa-arrow-up"></i>
            </li>
        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
    </ul>
</div>

    </div>

    <div class="zoom-in-image-mask">
    <img class="zoom-in-image">
</div>


    

</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/header-shrink.js"></script>

<script src="/js/back2top.js"></script>

<script src="/js/dark-light-toggle.js"></script>







    
<script src="/js/code-block.js"></script>





<div class="post-scripts">
    
        
<script src="/js/post-helper.js"></script>

        
        
    
</div>



</body>
</html>
